/**
 * Created by Taller on 2017/8/12.
 */
var component = {
    button:function (_button) {
        var html = '';
        if(_button.length > 0){
            for(var i=0;i<_button.length;i++){
                html += create_button(_button[i]);
            }
        }else {
            html = create_button(_button);
        }
        return html;
    },
    menu:function (_menu) {
        return create_menu(_menu);
    },
    table:function (_table) {
        return create_table(_table);
    }
}
//创建按钮组件
function create_button(_config) {
    var color = (typeof _config.color) == 'undefined' ? 'mdui-color-grey-700' : _config.color;
    var txt = (typeof _config.title) == 'undefined' ? 'Button' : _config.color;
    return '<button class="mdui-btn mdui-btn-raised '+color+'">'+txt+'</button>'
}
//创建菜单组件
function create_menu(_config) {
    var menu = '';
    for(var i=0;i<_config.length;i++){
        menu += '<li class="mdui-list-item mdui-ripple" data-url="'+_config[i].url+'">'+
                    '<div class="mdui-list-item-avatar">'+
                        '<i class="mdui-icon material-icons">'+_config[i].icon+'</i>'+
                    '</div>'+
                    '<div class="mdui-list-item-content">'+
                        '<div class="mdui-list-item-title">'+_config[i].title+'</div>'+
                    '</div>'+
                '</li>'+
                '<li class="mdui-divider-inset mdui-m-y-0"></li>';
    }
    return menu;
}
//创建表格
function create_table(_config) {


    var request = ajax.send({url:'/admin/menu/test/menu'});
    request.done(function (data) {
        var item = data.result;
        var tr = '';
        var thead = _config.thead;
        var th = create_table_thead(thead);
        for(var i=0;i<item.length;i++){
            var td = '';
            for(var j=0;j<thead.length;j++){
                var obj = item[i];
                td += '<td>'+obj[thead[j].field]+'</td>'
            }
            tr += '<tr>'+td+'</tr>'
        }
        var table = '<table class="mdui-table mdui-table-selectable mdui-table-hoverable">' +
            th+
            '<tbody>'+tr+'</tbody>' +
            '</table>'
        $(_config.id).html(table);
        //分页初始化
        var page = {total:data.total,fn:_config.fn};
        initPage(page);
        drawing();
    })


}
function create_table_thead(_thead) {
    var th = '';
    for(var i=0;i<_thead.length;i++){
        var tooltip = (typeof _thead[i].tip) == 'undefined' ? '' : "mdui-tooltip=\"{ position: 'top',content:'"+_thead[i].tip+"'}\"";
        th += '<th '+tooltip+'>'+_thead[i].title+'</th>'
    }
    return '<thead><tr>'+th+'</tr></thead>'
}

